package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketOrderGoods;
import com.cskaoyan.market.service.wx.WxOrderService;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.wx.lhh.OrderInfoVo;
import com.cskaoyan.market.vo.wx.lhh.WxOrderVo;
import com.cskaoyan.market.vo.wx.lhh.homeIndexVo.CommentVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WxOrderController
 * @Description
 * @Author MZ
 * @Date 2023/5/19 20:30
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    /**
     * @MethodName list
     * @Description 订单列表的现实    showType=4&page=1&limit=10
     * @param: page    页码
     * @param: limit    数量
     * @param: showType 显示类型
     * @return: java.lang.Object
     * @Author MZ
     * @Date 20:36 2023/5/19
     **/
    @GetMapping("list")
    public Object list(Integer page, Integer limit, Integer showType) {
        List<Short> orderStatus = new ArrayList<>();
        if (showType == 0) {
            // 全部
            orderStatus.add(MarketConstant.NO_PAYMENT);
            orderStatus.add(MarketConstant.USER_CANCEL);
            orderStatus.add(MarketConstant.SYSTEM_CANCEL);
            orderStatus.add(MarketConstant.HAVE_PAYMENT);
            orderStatus.add(MarketConstant.APPLY_REFUND);
            orderStatus.add(MarketConstant.HAVE_REFUND);
            orderStatus.add(MarketConstant.HAVE_SHIPMENT);
            orderStatus.add(MarketConstant.USER_RECEIPT);
            orderStatus.add(MarketConstant.SYSTEM_RECEIPT);
        } else if (showType == 1) {
            // 找未付款
            orderStatus.add(MarketConstant.NO_PAYMENT);
        } else if (showType == 2) {
            // 找已付款
            orderStatus.add(MarketConstant.HAVE_PAYMENT);
        } else if (showType == 3) {
            // 找已发货
            orderStatus.add(MarketConstant.HAVE_SHIPMENT);
        } else if (showType == 4) {
            // 找已收货
            orderStatus.add(MarketConstant.USER_RECEIPT);
            orderStatus.add(MarketConstant.SYSTEM_RECEIPT);
        }
        List<WxOrderVo> wxOrderList = wxOrderService.orderList(orderStatus, page, limit);

        return ResponseUtil.okList(wxOrderList);
    }

    /**
     * @MethodName detail
     * @Description 订单详情页
     * @param: orderId  订单id
     * @return: java.lang.Object
     * @Author MZ
     * @Date 14:09 2023/5/20
     **/
    @GetMapping("detail")
    public Object detail(Integer orderId) {

        Map<String, Object> result = new HashMap<>();
        String[] expressInfo = new String[]{};
        result.put("expressInfo", expressInfo);
        // orderInfo    getOrderInfo
        OrderInfoVo orderInfo = wxOrderService.getOrderInfo(orderId);
        // orderGoods   getOrderGoodsList
        List<MarketOrderGoods> orderGoods = wxOrderService.getOrderGoodsList(orderId);

        result.put("orderInfo", orderInfo);
        result.put("orderGoods", orderGoods);

        return ResponseUtil.ok(result);
    }

    /**
     * @MethodName cancel
     * @Description 取消订单
     * @param: map {"orderId":15}
     * @return: java.lang.Object
     * @Author MZ
     * @Date 17:18 2023/5/20
     **/
    @PostMapping("cancel")
    public Object cancel(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");

        int row = wxOrderService.cancelOrderByOrderId(orderId);

        if (row != 1) {
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }

    /**
     * @MethodName refund
     * @Description 申请退款
     * @param: map
     * @return: java.lang.Object
     * @Author MZ
     * @Date 17:49 2023/5/20
     **/
    @PostMapping("refund")
    public Object refund(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");

        int row = wxOrderService.refundOrderByOrderId(orderId);

        if (row != 1) {
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }

    /**
     * @MethodName delete
     * @Description 删除订单
     * @param: map
     * @return: java.lang.Object
     * @Author MZ
     * @Date 17:52 2023/5/20
     **/
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");

        int row = wxOrderService.deleteOrderByOrderId(orderId);

        if (row != 1) {
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }

    /**
     * @MethodName confirm
     * @Description 确认收获
     * @param: map
     * @return: java.lang.Object
     * @Author MZ
     * @Date 17:52 2023/5/20
     **/
    @PostMapping("confirm")
    public Object confirm(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");

        int row = wxOrderService.confirmOrderByOrderId(orderId);

        if (row != 1) {
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }


    /**
     * @MethodName goods
     * @Description 订单商品页面
     * @param: orderId
     * @param: goodsId
     * @return: java.lang.Object
     * @Author MZ
     * @Date 23:49 2023/5/20
     **/
    @GetMapping("goods")
    public Object goods(Integer orderId, Integer goodsId) {
        // orderId=167&goodsId=1135002
        MarketOrderGoods orderGoods = wxOrderService.getOrderGoods(orderId, goodsId);
        return ResponseUtil.ok(orderGoods);
    }


    /**
     * @MethodName comment
     * @Description 发表评论
     * @param: commentVo
     * @return: java.lang.Object
     * @Author MZ
     * @Date 23:49 2023/5/20
     **/
    @PostMapping("comment")
    public Object comment(@RequestBody CommentVo commentVo) {
        Integer orderGoodsId = commentVo.getOrderGoodsId();
        String content = commentVo.getContent();
        Short star = commentVo.getStar();
        Boolean hasPicture = commentVo.getHasPicture();
        String[] picUrls = commentVo.getPicUrls();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        int row = wxOrderService.commentOrderGoods(orderGoodsId, content, star, hasPicture, picUrls, userId);
        if (row != 1) {
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok();
    }

    /**
     * @MethodName orderPay
     * @Description  提交订单
     * @param: map
     * @return: java.lang.Object
     * @Author MZ
     * @Date 16:54 2023/5/22
     **/
    @PostMapping("submit")
    public Object orderPay(@RequestBody Map<String, Object> map) {
        // {"cartId":0,"addressId":23,"couponId":-1,"userCouponId":-1,"message":"","grouponRulesId":0,"grouponLinkId":0}
        Integer cartId = (Integer) map.get("cartId");
        Integer addressId = (Integer) map.get("addressId");
        Integer couponId = (Integer) map.get("couponId");
        Integer userCouponId = (Integer) map.get("userCouponId");
        String message = (String) map.get("message");
        Integer grouponRulesId = (Integer) map.get("grouponRulesId");
        Integer grouponLinkId = (Integer) map.get("grouponLinkId");

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        Integer orderId = wxOrderService.submitOrder(cartId, addressId, couponId, userCouponId, message, grouponRulesId, grouponLinkId, userId);

        Map<String, Integer> result = new HashMap<>();
        result.put("orderId", orderId);
        result.put("grouponLinkId", grouponLinkId);

        return ResponseUtil.ok(result);
    }

    /**
     * @MethodName prepay
     * @Description  付款
     * @param: map
     * @return: java.lang.Object
     * @Author MZ
     * @Date 16:54 2023/5/22
     **/
    @PostMapping("prepay")
    public Object prepay(@RequestBody Map<String, Integer> map) {

        Integer orderId = map.get("orderId");

        Integer row = wxOrderService.prepayOrder(orderId);

        if (row != 1){
            return ResponseUtil.badArgument();
        }

        return ResponseUtil.ok();
    }


}
